package org.common4j2ee.stock.web.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextImpl;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

@SuppressWarnings("serial")
public abstract class CrudActionSupport<T> extends ActionSupport implements
		ModelDriven<T>, Preparable {

	public static final String RELOAD = "reload";

	protected Logger logger = LoggerFactory.getLogger(getClass());

	protected String getUserName() {
		Object content=Struts2Utils
		.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
		if(content==null){
			return null;
		}
		SecurityContextImpl securityContent = (SecurityContextImpl) content;
		Authentication auth=securityContent.getAuthentication();
		if(auth==null){
			return null;
		}
		String userName = auth.getName();
		return userName;
	}

	public abstract String list() throws Exception;

	@Override
	public abstract String input() throws Exception;

	public abstract String save() throws Exception;

	public abstract String delete() throws Exception;

	public void prepare() throws Exception {
	}

	public void prepareInput() throws Exception {
		prepareModel();
	}

	public void prepareSave() throws Exception {
		prepareModel();
	}

	protected abstract void prepareModel() throws Exception;
}
